import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:juming_test/model/domain/domain_filter_item_model.dart';
import 'package:juming_test/model/domain/domain_filter_option_model.dart';
import 'package:juming_test/model/domain/domain_filter_section_model.dart';
import 'package:juming_test/module/domain/domain_filter/widgets/domain_filter_checkbox.dart';
import 'package:juming_test/module/domain/domain_filter/widgets/domain_filter_multiple_input.dart';
import 'package:juming_test/module/domain/domain_filter/widgets/domain_filter_option.dart';
import 'package:juming_test/module/domain/domain_filter/widgets/domain_filter_option_group.dart';
import 'package:juming_test/module/domain/domain_filter/widgets/domain_filter_picker.dart';
import 'package:juming_test/module/domain/domain_filter/widgets/domain_filter_single_input.dart';

class DomainFilterSubList extends StatelessWidget {
  const DomainFilterSubList({
    super.key,
    required this.section,
    required this.onValueChanged,
  });

  // 域名筛选列表
  final DomainFilterSectionModel section;
  // 事件
  // 用户操作
  final Function(String key, String value) onValueChanged;

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: section.children.length,
      itemBuilder: (context, index) {
        final item =
            (index >= 0 && index < section.children.length)
                ? section.children[index]
                : null;
        if (item == null) return SizedBox();
        return Padding(
          key: Key(item.key),
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                item.name,
                style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.bold),
              ),
              SizedBox(height: 8.w),
              _buildWidgetFromConfig(item),
            ],
          ),
        );
      },
    );
  }

  // 根据数据配置渲染Widget
  Widget _buildWidgetFromConfig(DomainFilterItemModel item) {
    switch (item.type) {
      // 单输入框带复选框
      case DomainFilterItemType.singleInput:
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            DomainFilterSingleInput(
              model: item,
              onInputCompleted: (value) {
                if (value.isEmpty) {
                  onValueChanged(item.key, '-1');
                } else {
                  onValueChanged(item.key, value);
                }
              },
            ),
            SizedBox(height: 8.w),
            DomainFilterCheckbox(
              list: item.options,
              onChanged: (list) {
                for (var element in item.options) {
                  if (!list.contains(element)) {
                    onValueChanged(element.key, '-1');
                  } else {
                    onValueChanged(element.key, '1');
                  }
                }
              },
            ),
          ],
        );
      case DomainFilterItemType.doubleInput:
        return DomainFilterMultipleInput(
          model: item,
          onInput1Completed: ((value) {
            if (value.isEmpty) {
              onValueChanged(item.inputKey1, '-1');
            } else {
              onValueChanged(item.inputKey1, value);
            }
          }),
          onInput2Completed: ((value) {
            if (value.isEmpty) {
              onValueChanged(item.inputKey2, '-1');
            } else {
              onValueChanged(item.inputKey2, value);
            }
          }),
        );
      // 单选带输入框
      case DomainFilterItemType.singleChoiceWithInput:
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            DomainFilterOption(
              model: item,
              onChanged: (list) {
                if (list.isEmpty) {
                  onValueChanged(item.key, '-1');
                } else {
                  onValueChanged(item.key, list.map((e) => e.value).join(','));
                }
              },
            ),
            SizedBox(height: 8.w),
            Text(
              "自定义",
              style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 8.w),
            DomainFilterMultipleInput(
              model: item,
              onInput1Completed: (value) {
                if (value.isEmpty) {
                  onValueChanged(item.inputKey1, '-1');
                } else {
                  onValueChanged(item.inputKey1, value);
                }
              },
              onInput2Completed: (value) {
                if (value.isEmpty) {
                  onValueChanged(item.inputKey2, '-1');
                } else {
                  onValueChanged(item.inputKey2, value);
                }
              },
            ),
          ],
        );
      // 群组单选
      case DomainFilterItemType.groupSingleChoice:
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            DomainFilterOptionGroup(
              list: item.groupItems,
              onChanged: (list) {
                if (list.isEmpty) {
                  onValueChanged(item.key, '-1');
                } else {
                  onValueChanged(item.key, list.map((e) => e.value).join(','));
                }
              },
            ),
          ],
        );
      // 单选&多选
      case DomainFilterItemType.singleChoice:
      case DomainFilterItemType.multipleChoice:
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            DomainFilterOption(
              model: item,
              onChanged: (list) {
                if (list.isEmpty) {
                  onValueChanged(item.key, '-1');
                } else {
                  onValueChanged(item.key, list.map((e) => e.value).join(','));
                }
              },
            ),
            _checkShowDerivative(item),
          ],
        );
      // 双选择器
      case DomainFilterItemType.doublePicker:
        return DomainFilterPicker(
          model: item,
          onPick1Completed: (value) {
            onValueChanged(item.inputKey1, value);
          },
          onPick2Completed: (value) {
            onValueChanged(item.inputKey2, value);
          },
        );
    }
  }

  // 检查选项衍生条目
  Widget _checkShowDerivative(DomainFilterItemModel item) {
    if (item.derivativeValue.isNotEmpty) {
      final DomainFilterOptionModel option = item.options.firstWhere(
        (element) =>
            element.isSelected && element.value == item.derivativeValue,
        orElse: () => DomainFilterOptionModel.fromJson({}),
      );
      if (option.id == 0) {
        return const SizedBox();
      }
      return Container(
        margin: EdgeInsets.only(top: 8.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children:
              item.derivativeItems.map((e) {
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      e.name,
                      style: TextStyle(
                        fontSize: 16.sp,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 8.w),
                    _buildWidgetFromConfig(e),
                  ],
                );
              }).toList(),
        ),
      );
    } else {
      return SizedBox();
    }
  }
}
